﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using Assist;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YJKRevitFamilyDataBase;
using YJKExcelFormTool.Creator;
using YJKExcelFormTool.DataDefine;

namespace YJKExcelFormTool.Forms
{
	public partial class MaterialTableForm : System.Windows.Forms.Form
	{
		public TableInfos TheTableInfos
		{
			get
			{
				return this.m_CommonForm.TheTableInfos;
			}
		}

		public MaterialTableForm(ExternalCommandData cmdData, string tableSettingFile, MaterialProgramInfos._Tag_TemplateType templateType, SubjectType subjectType, string programId)
		{
			this.m_Revit = cmdData;
			this.m_TableSettingFile = tableSettingFile;
			this.m_TemplateType = templateType;
			this.m_SubjectType = subjectType;
			this.m_ProgramId = programId;
			if (this.m_ProgramId.CompareTo("") == 0)
			{
				this.m_ProgramId = Guid.NewGuid().ToString();
			}
			this.InitializeComponent();
		}

		private void MaterialTableForm_Load(object sender, EventArgs e)
		{
			this.InitData();
			if (this.m_SubjectType == SubjectType.Electric)
			{
				this.tabControl.Controls.Remove(this.tabPageFilter);
			}
		}

		private void MaterialTableForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (base.DialogResult == DialogResult.OK)
			{
				string text = "";
				if (!this.SaveData(ref text))
				{
					if (text.CompareTo("") != 0)
					{
						YJKMessageBox.Show(text);
					}
					e.Cancel = true;
				}
			}
		}

		private void clbCategoriesList_SelectedIndexChanged(object sender, EventArgs e)
		{
			CheckedListBox checkedListBox = sender as CheckedListBox;
			if (!checkedListBox.GetItemChecked(checkedListBox.SelectedIndex))
			{
				string itemText = checkedListBox.GetItemText(checkedListBox.SelectedItem);
				foreach (TableHeadCell tableHeadCell in this.m_MaterialProgramInfos.TableInfos.Head.GetEffectiveRow())
				{
					tableHeadCell.Key.RemoveAttributesByCategory(itemText);
				}
			}
		}

		private void btnAllSelection_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.clbCategoriesList.Items.Count; i++)
			{
				this.clbCategoriesList.SetItemChecked(i, true);
			}
		}

		private void btnNullSelection_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.clbCategoriesList.Items.Count; i++)
			{
				this.clbCategoriesList.SetItemChecked(i, false);
			}
		}

		private void btnInvertSelection_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.clbCategoriesList.Items.Count; i++)
			{
				bool value = !this.clbCategoriesList.GetItemChecked(i);
				this.clbCategoriesList.SetItemChecked(i, value);
			}
		}

		private void cmbBoxFilterName1_SelectedIndexChanged(object sender, EventArgs e)
		{
			int num = this.m_FilterConditionalList.FindIndex((MaterialTableForm.FilterConditional s) => s.Name.CompareTo(this.cmbBoxFilterName1.Text) == 0);
			if (num == -1)
			{
				return;
			}
			this.cmbBoxFilterCondition1.Items.Clear();
			foreach (string item in this.m_FilterConditionalList[num].ConditionalList)
			{
				this.cmbBoxFilterCondition1.Items.Add(item);
			}
		}

		private void cmbBoxFilterName2_SelectedIndexChanged(object sender, EventArgs e)
		{
			int num = this.m_FilterConditionalList.FindIndex((MaterialTableForm.FilterConditional s) => s.Name.CompareTo(this.cmbBoxFilterName2.Text) == 0);
			if (num == -1)
			{
				return;
			}
			this.cmbBoxFilterCondition2.Items.Clear();
			foreach (string item in this.m_FilterConditionalList[num].ConditionalList)
			{
				this.cmbBoxFilterCondition2.Items.Add(item);
			}
		}

		private void cmbBoxFilterName3_SelectedIndexChanged(object sender, EventArgs e)
		{
			int num = this.m_FilterConditionalList.FindIndex((MaterialTableForm.FilterConditional s) => s.Name.CompareTo(this.cmbBoxFilterName3.Text) == 0);
			if (num == -1)
			{
				return;
			}
			this.cmbBoxFilterCondition3.Items.Clear();
			foreach (string item in this.m_FilterConditionalList[num].ConditionalList)
			{
				this.cmbBoxFilterCondition3.Items.Add(item);
			}
		}

		private void cmbBoxFilterName4_SelectedIndexChanged(object sender, EventArgs e)
		{
			int num = this.m_FilterConditionalList.FindIndex((MaterialTableForm.FilterConditional s) => s.Name.CompareTo(this.cmbBoxFilterName4.Text) == 0);
			if (num == -1)
			{
				return;
			}
			this.cmbBoxFilterCondition4.Items.Clear();
			foreach (string item in this.m_FilterConditionalList[num].ConditionalList)
			{
				this.cmbBoxFilterCondition4.Items.Add(item);
			}
		}

		private void InitData()
		{
			if (new MaterialTableCreater(this.m_Revit).ReadTableSetting(this.m_TableSettingFile, this.m_ProgramId, ref this.m_MaterialProgramInfos))
			{
				this.m_TemplatePath = this.m_MaterialProgramInfos.TableInfos.Head.TableSourceInfo.Name;
				this.tbProgramName.Text = this.m_MaterialProgramInfos.Name;
				this.tbDescribe.Text = this.m_MaterialProgramInfos.Describe;
			}
			else
			{
				this.m_MaterialProgramInfos = new MaterialProgramInfos();
				this.tbProgramName.Text = "";
				this.tbDescribe.Text = "";
			}
			if (this.ReadCategoryInfos())
			{
				using (List<CombinationOfAttributes>.Enumerator enumerator = this.m_CategoryInfos.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						CombinationOfAttributes combOfAttributes = enumerator.Current;
						if (combOfAttributes.AttributesName.Count >= 1)
						{
							bool isChecked = true;
							if (this.m_MaterialProgramInfos.StatisticalCategoryList.Count > 0)
							{
								int num = this.m_MaterialProgramInfos.StatisticalCategoryList.FindIndex((MaterialProgramInfos.StatisticalCategory s) => s.m_Name.CompareTo(combOfAttributes.CategoryName) == 0);
								if (num != -1)
								{
									isChecked = this.m_MaterialProgramInfos.StatisticalCategoryList[num].m_Checked;
								}
							}
							this.clbCategoriesList.Items.Add(combOfAttributes.CategoryName, isChecked);
						}
					}
				}
			}
			this.m_CommonForm = new MaterialTableCommonForm(this.m_Revit, this.m_MaterialProgramInfos.TableInfos, this.clbCategoriesList, this.m_CategoryInfos, this.m_TemplateType, this.m_TemplatePath, this.m_SubjectType);
			this.m_CommonForm.TopLevel = false;
			this.m_CommonForm.Dock = DockStyle.Fill;
			this.m_CommonForm.FormBorderStyle = FormBorderStyle.None;
			this.tabControl.SelectedIndex = 0;
			this.tabPageTableDesign.Controls.Add(this.m_CommonForm);
			this.m_CommonForm.Show();
			if (this.ReadFilterInfos())
			{
				for (int i = 0; i < this.m_MaterialProgramInfos.FilterTypeList.Count; i++)
				{
					MaterialProgramInfos.FilterType filterType = this.m_MaterialProgramInfos.FilterTypeList[i];
					switch (i)
					{
					case 0:
						this.cmbBoxFilterName1.Text = filterType.m_Type;
						this.cmbBoxFilterCondition1.Text = filterType.m_Condition;
						this.cmbBoxFilterValue1.Text = filterType.m_Value;
						break;
					case 1:
						this.cmbBoxFilterName2.Text = filterType.m_Type;
						this.cmbBoxFilterCondition2.Text = filterType.m_Condition;
						this.cmbBoxFilterValue2.Text = filterType.m_Value;
						break;
					case 2:
						this.cmbBoxFilterName3.Text = filterType.m_Type;
						this.cmbBoxFilterCondition3.Text = filterType.m_Condition;
						this.cmbBoxFilterValue3.Text = filterType.m_Value;
						break;
					case 3:
						this.cmbBoxFilterName4.Text = filterType.m_Type;
						this.cmbBoxFilterCondition4.Text = filterType.m_Condition;
						this.cmbBoxFilterValue4.Text = filterType.m_Value;
						break;
					}
				}
			}
		}

		private bool SaveData(ref string message)
		{
			if (!this.SaveBasicInfo(ref message))
			{
				return false;
			}
			List<MaterialProgramInfos.StatisticalCategory> list = new List<MaterialProgramInfos.StatisticalCategory>();
			bool flag = false;
			for (int i = 0; i < this.clbCategoriesList.Items.Count; i++)
			{
				MaterialProgramInfos.StatisticalCategory statisticalCategory = new MaterialProgramInfos.StatisticalCategory
				{
					m_Name = this.clbCategoriesList.GetItemText(this.clbCategoriesList.Items[i]),
					m_Checked = this.clbCategoriesList.GetItemChecked(i)
				};
				if (statisticalCategory.m_Checked)
				{
					flag = true;
				}
				list.Add(statisticalCategory);
			}
			if (!flag)
			{
				message = "统计类别为空";
				return false;
			}
			this.m_MaterialProgramInfos.StatisticalCategoryList = list;
			if (this.m_CommonForm.TheTableInfos.Head.IsNull())
			{
				message = "表头设计为空";
				return false;
			}
			this.m_MaterialProgramInfos.TableInfos = this.m_CommonForm.TheTableInfos;
			this.SaveFilterInfo();
			new MaterialTableCreater(this.m_Revit).WriteTableSetting(this.m_MaterialProgramInfos, this.m_TableSettingFile, this.m_ProgramId);
			return true;
		}

		private bool SaveBasicInfo(ref string message)
		{
			bool isExcelTemplate = false;
			if (this.m_MaterialProgramInfos.TableInfos.Head.HeadType == TableHead._Tag_HeadType.HT_EXCEL)
			{
				isExcelTemplate = true;
			}
			TableInterface.GetProgramInfoList(this.m_SubjectType, isExcelTemplate, this.m_Revit);
			string text = this.tbProgramName.Text.Trim();
			if (text.CompareTo("") == 0)
			{
				message = "方案名称为空";
				return false;
			}
			this.m_MaterialProgramInfos.Name = text;
			this.m_MaterialProgramInfos.Describe = this.tbDescribe.Text;
			this.m_MaterialProgramInfos.Id = this.m_ProgramId;
			return true;
		}

		private void SaveFilterInfo()
		{
			List<MaterialProgramInfos.FilterType> list = new List<MaterialProgramInfos.FilterType>();
			string text = this.cmbBoxFilterName1.Text;
			string text2 = this.cmbBoxFilterCondition1.Text;
			string filterValue = this.cmbBoxFilterValue1.Text.Trim();
			MaterialProgramInfos.FilterType item = default(MaterialProgramInfos.FilterType);
			if (this.GetFilterInfo(text, text2, filterValue, ref item))
			{
				list.Add(item);
			}
			text = this.cmbBoxFilterName2.Text;
			text2 = this.cmbBoxFilterCondition2.Text;
			filterValue = this.cmbBoxFilterValue2.Text.Trim();
			MaterialProgramInfos.FilterType item2 = default(MaterialProgramInfos.FilterType);
			if (this.GetFilterInfo(text, text2, filterValue, ref item2))
			{
				list.Add(item2);
			}
			text = this.cmbBoxFilterName3.Text;
			text2 = this.cmbBoxFilterCondition3.Text;
			filterValue = this.cmbBoxFilterValue3.Text.Trim();
			MaterialProgramInfos.FilterType item3 = default(MaterialProgramInfos.FilterType);
			if (this.GetFilterInfo(text, text2, filterValue, ref item3))
			{
				list.Add(item3);
			}
			text = this.cmbBoxFilterName4.Text;
			text2 = this.cmbBoxFilterCondition4.Text;
			filterValue = this.cmbBoxFilterValue4.Text.Trim();
			MaterialProgramInfos.FilterType item4 = default(MaterialProgramInfos.FilterType);
			if (this.GetFilterInfo(text, text2, filterValue, ref item4))
			{
				list.Add(item4);
			}
			this.m_MaterialProgramInfos.FilterTypeList.Clear();
			this.m_MaterialProgramInfos.FilterTypeList.AddRange(list);
		}

		private bool GetFilterInfo(string filterName, string filterConditional, string filterValue, ref MaterialProgramInfos.FilterType filterType)
		{
			if (filterName.CompareTo("") != 0 && filterConditional.CompareTo("") != 0 && filterValue.CompareTo("") != 0)
			{
				filterType.m_Type = filterName;
				filterType.m_Condition = filterConditional;
				filterType.m_Value = filterValue;
				return true;
			}
			return false;
		}

		private string GetCategoryInfosFileName()
		{
			string text;
			if (this.m_SubjectType == SubjectType.Electric)
			{
				text = Product.WorkingLocation + "ElectricCategoryInfos.xml";
			}
			else if (this.m_SubjectType == SubjectType.HVAC)
			{
				text = Product.WorkingLocation + "HVACCategoryInfos.xml";
			}
			else
			{
				text = Product.WorkingLocation + "PlumbingCategoryInfos.xml";
			}
			if (!File.Exists(text))
			{
				string sourceFileName;
				if (this.m_SubjectType == SubjectType.Electric)
				{
					sourceFileName = AssistFunc.GetAppRootPath() + "\\Data\\ElectricalData\\ElectricCategoryInfos.xml";
				}
				else if (this.m_SubjectType == SubjectType.HVAC)
				{
					sourceFileName = AssistFunc.GetAppRootPath() + "\\Data\\HVACData\\HVACCategoryInfos.xml";
				}
				else
				{
					sourceFileName = AssistFunc.GetAppRootPath() + "\\Data\\PlumbingData\\PlumbingCategoryInfos.xml";
				}
				this.CopyFile(sourceFileName, text);
			}
			return text;
		}

		private void CopyFile(string sourceFileName, string destFileName)
		{
			try
			{
				File.Copy(sourceFileName, destFileName);
			}
			catch (Exception)
			{
			}
		}

		private bool ReadCategoryInfos()
		{
			string categoryInfosFileName = this.GetCategoryInfosFileName();
			XmlDocument xmlDocument = new XmlDocument();
			try
			{
				xmlDocument.Load(categoryInfosFileName);
			}
			catch (Exception ex)
			{
				ex.ToString();
				YJKMessageBox.Show("缺少配置文件：" + categoryInfosFileName);
				return false;
			}
			foreach (object obj in xmlDocument.SelectSingleNode("/Root/Header").ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				List<string> attributesName = new List<string>();
				List<string> delimiter = new List<string>();
				CombinationOfAttributes item = new CombinationOfAttributes(xmlNode.InnerText, attributesName, delimiter);
				this.m_CategoryInfos.Add(item);
			}
			foreach (object obj2 in xmlDocument.SelectSingleNode("/Root/Categories").ChildNodes)
			{
				XmlNode xmlNode2 = (XmlNode)obj2;
				string attribute = ((XmlElement)xmlNode2).GetAttribute("Name");
				XmlNodeList childNodes = xmlNode2.ChildNodes;
				List<string> list = new List<string>();
				foreach (object obj3 in childNodes)
				{
					string attribute2 = ((XmlElement)((XmlNode)obj3)).GetAttribute("Title");
					list.Add(attribute2);
				}
				List<string> delimiter2 = new List<string>();
				CombinationOfAttributes item2 = new CombinationOfAttributes(attribute, list, delimiter2);
				this.m_CategoryInfos.Add(item2);
			}
			return true;
		}

		private bool ReadFilterInfos()
		{
			string categoryInfosFileName = this.GetCategoryInfosFileName();
			XmlDocument xmlDocument = new XmlDocument();
			try
			{
				xmlDocument.Load(categoryInfosFileName);
			}
			catch (Exception ex)
			{
				ex.ToString();
				YJKMessageBox.Show("缺少配置文件：" + categoryInfosFileName);
				return false;
			}
			XmlNode xmlNode = xmlDocument.SelectSingleNode("/Root/Filters");
			if (xmlNode != null)
			{
				foreach (object obj in xmlNode.ChildNodes)
				{
					XmlNode xmlNode2 = (XmlNode)obj;
					string attribute = ((XmlElement)xmlNode2).GetAttribute("Name");
					XmlNodeList childNodes = xmlNode2.ChildNodes;
					List<string> list = new List<string>();
					foreach (object obj2 in childNodes)
					{
						string innerText = ((XmlNode)obj2).InnerText;
						list.Add(innerText);
					}
					MaterialTableForm.FilterConditional item = new MaterialTableForm.FilterConditional(attribute, list);
					this.m_FilterConditionalList.Add(item);
				}
				foreach (MaterialTableForm.FilterConditional filterConditional in this.m_FilterConditionalList)
				{
					this.cmbBoxFilterName1.Items.Add(filterConditional.Name);
					this.cmbBoxFilterName2.Items.Add(filterConditional.Name);
					this.cmbBoxFilterName3.Items.Add(filterConditional.Name);
					this.cmbBoxFilterName4.Items.Add(filterConditional.Name);
				}
			}
			return true;
		}

		private ExternalCommandData m_Revit;

		private MaterialTableCommonForm m_CommonForm;

		private MaterialProgramInfos._Tag_TemplateType m_TemplateType;

		private string m_ProgramId;

		private MaterialProgramInfos m_MaterialProgramInfos;

		private List<CombinationOfAttributes> m_CategoryInfos = new List<CombinationOfAttributes>();

		private List<MaterialTableForm.FilterConditional> m_FilterConditionalList = new List<MaterialTableForm.FilterConditional>();

		private SubjectType m_SubjectType;

		private string m_TableSettingFile = "";

		private string m_TemplatePath = "";

		private class FilterConditional
		{
			public string Name
			{
				get
				{
					return this.m_Name;
				}
			}

			public List<string> ConditionalList
			{
				get
				{
					return this.m_ConditionalList;
				}
			}

			public FilterConditional(MaterialTableForm.FilterConditional filterConditional)
			{
				this.m_Name = filterConditional.m_Name;
				this.m_ConditionalList.Clear();
				this.m_ConditionalList.AddRange(filterConditional.m_ConditionalList);
			}

			public FilterConditional(string name, List<string> conditionalLis)
			{
				this.m_Name = name;
				this.m_ConditionalList.Clear();
				this.m_ConditionalList.AddRange(conditionalLis);
			}

			private string m_Name = "";

			private List<string> m_ConditionalList = new List<string>();
		}
	}
}
